<?php

namespace common\biz;

/**
 * 业务逻辑层-账号
 *
 * @author peng.huang
 */
class AccountBiz extends \common\base\BaseBiz {

    /**
     * 构造函数
     */
    public function __construct() {
        $this->dao = static::getDao('common\dao\AccountDao');
    }

    /**
     * 判断账号是否存在[不验证账号是否有效]
     * 
     * @param int $aid [账号ID]
     * @param string $loginName [登录名称]
     * @return boolean
     */
    public function isExists($aid, $loginName = NULL) {
        $where = array();
        if ($aid > 0) {
            $where = array('id' => (int) $aid);
        } else {
            // 验证[手机\Email\普通用户]
            $where = array(
                'mobile' => $loginName,
                ' OR email' => $loginName,
                ' OR username' => $loginName,
            );
        }

        return $this->dao->isExists($where);
    }

    /**
     * 登录
     * 
     * @param object $entity [实体对象]
     * @return boolean
     */
    public function login($entity) {
        $checkfield = $this->_getCheckField($entity);
        $row = $this->dao->findOne('id,password', array('status' => 1, $checkfield => $entity->loginname));
        if (empty($row) || md5($entity->password) !== $row['password']) {
            return $entity->appendError('password', '该账号或者密码错误。');
        }

        $entity->aid = $row['id'];
        return TRUE;
    }

    /**
     * 无密码登录
     * 
     * @param object $entity [实体对象]
     * @return boolean
     */
    public function loginWithout($entity) {
        $checkfield = $this->_getCheckField($entity);
        $account = $this->dao->findOne('id,status', array($checkfield => $entity->loginname));
        if (empty($account)) {
            $data = array('rtype' => $entity->rtype, 'utype' => $entity->utype, 'status' => 1, $checkfield => $entity->loginname, 'password' => md5($entity->password));
            if (-1 === ($aid = $this->dao->save($data))) {
                return $entity->appendError('loginname', '注册失败。');
            }
        } elseif ('1' != $account['status']) {
            return $entity->appendError('loginname', '该账号已经被停用。');
        }

        $aid = $account['id'];
        $entity->aid = $aid;
        return TRUE;
    }

    /**
     * 注册账号
     * 
     * @param object $entity [实体对象]
     * @return boolean
     */
    public function signup($entity) {
        if ($this->isExists(-1, $entity->loginname)) {
            return $entity->appendError('loginname', '该账号已经注册。');
        }

        $checkfield = $this->_getCheckField($entity);
        $data = array('rtype' => $entity->rtype, 'utype' => $entity->utype, 'status' => 1, $checkfield => $entity->loginname, 'password' => md5($entity->password));
        if (-1 === ($aid = $this->dao->save($data))) {
            return $entity->appendError('loginname', '注册失败。');
        }

        $entity->aid = $aid;
        return TRUE;
    }

    /**
     * 修改密码不需要旧密码
     * 
     * @param object $entity [实体对象]
     * @return boolean 
     */
    public function modifyPwdWithNoOldpwd($entity) {
        $checkfield = $this->_getCheckField($entity);
        $account = $this->dao->findOne('id', array('status' => 1, $checkfield => $entity->loginname));
        if (empty($account)) {
            return $entity->appendError('loginname', '该账号或密码错误。');
        } elseif (-1 === $this->dao->save(array('id' => $account['id'], 'password' => md5($entity->password)))) {
            return $entity->appendError('password', '修改密码失败。');
        }

        return TRUE;
    }

    /**
     * 修改密码
     * 
     * @param object $entity [实体对象]
     * @return boolean 
     */
    public function modifyPwd($entity) {
        $account = $this->dao->findOne('password', array('status' => 1, 'id' => $entity->aid));
        if (empty($account)) {
            return $entity->appendError('aid', '该账号或密码错误。');
        } elseif ($account['password'] !== md5($entity->oldpwd)) {
            return $entity->appendError('oldpwd', '旧密码错误。');
        } elseif ($account['password'] === md5($entity->newpwd)) {
            return $entity->appendError('newpwd', '新密码不能与旧密码相同。');
        } elseif (-1 === $this->dao->save(array('id' => $entity->aid, 'password' => md5($entity->newpwd)))) {
            return $entity->appendError('newpwd', '修改密码失败。');
        }

        return TRUE;
    }

    /**
     * 返回验证字段
     * 
     * @param object $entity [实体对象]
     */
    protected function _getCheckField($entity) {
        // 登录类型
        switch ($entity->utype) {
            case 0: // 手机号
                return 'mobile';
            case 1: // EMAIL
                return 'email';
            default : // 普通用户
                return 'username';
        }
    }

}
